<template>
  <basic-container>
    <avue-crud ref="crud" v-model="form" :before-open="beforeOpen" :data="data" :option="option" :page.sync="page"
      :permission="permissionList" :table-loading="loading" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
      @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
      <template slot="sendStrectForm" slot-scope="{row}">
        <el-cascader :disabled="disabled" v-model="form.rcvStrectCascader" :props="props" :options="options"
          @change="handleChange"></el-cascader>
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入内容" v-model="form.sendStrect">
        </el-input>
      </template>
      <template slot="menu" slot-scope="{row}">
        <el-button type="text" @click="receiveHandler(row)" v-if="row.isSend == '1'">确认收取</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, updateIssend, remove } from "@/api/logistics_information/getFiel.js";
import { mapGetters } from "vuex";
import dept from '@/mock/dept.js'
export default {
  data() {
    return {
      disabled: false,
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        // selection: true,
        labelWidth: 180,
        indexLabel: '序号',
        addBtn: true,
        indexWidth: 80,
        refreshBtn: false,
        selection: false,  //多选框
        columnBtn: false,
        delBtn: false,
        editBtn: true,
        editBtnText: '重新寄送',
        searchShowBtn: false,
        searchLabelWidth: 150,
        column: [
          {
            label: "信封流水号",
            prop: "emsOrdNo",
            search: true,
            display: false,
          },
          {
            label: "办理机构",
            prop: "orgname",
            display: false,
          },
          {
            label: "收件人姓名",
            prop: "rcvName",
            search: true,
            display: false,
          },
          {
            label: "收件人手机号码",
            prop: "rcvPhone",
            display: false,
          },
          {
            label: "物流结果",
            prop: "emsstatus",
            display: false,
          },
          {
            label: "快递单号",
            prop: "mailNum",
            display: false,
          },
          {
            label: "办理部门代码",
            prop: "orgnameId",
            display: false,
          },
          {
            label: "寄送时间",
            prop: "sendtime",
            display: false,
          },
          {
            label: "发件人姓名",
            prop: "sendName",
            search: true,
            display: false,
          },
          {
            label: "是否寄送",
            prop: "issend",
            search: true,
            display: false,
          },
          {
            label: "创建时间",
            prop: "createDate",
            display: false,
          },
        ],
        group: [
          {
            icon: 'el-icon-info',
            label: '物流基本信息',
            collapse: true,
            prop: 'group1',
            column: [
              {
                label: '信封流水号',
                prop: 'emsOrdNo',
                rules: [{ required: true, message: "请输入信封流水号", trigger: "blur" }],
              },
              {
                label: '快递单号',
                prop: 'mailNum',
                rules: [{ required: true, message: "请输入快递单号", trigger: "blur" }],
              },
              {
                label: '受理台席名称',
                prop: 'govTbName',
                rules: [{ required: true, message: "请输入受理台席名称", trigger: "blur" }],
              },
              {
                label: '取件验证码',
                prop: 'chkCode',
                rules: [{ required: true, message: "请输入取件验证码", trigger: "blur" }],
              },
              {
                label: '办理部门',
                prop: 'orgname',
                type: 'select',
                dicUrl: "/apiblade-system/dept/getDeptByAreacode?areacode=320922",
                props: {
                  label: "deptName",
                  value: "institutionCode"
                },
                rules: [{ required: true, message: "请输入办理部门", trigger: "blur" }],
              },
              {
                label: '收费类型',
                prop: 'bussType',
                rules: [{ required: true, message: "请输入收费类型", trigger: "blur" }],
              },
              {
                label: '办理部门代码',
                prop: 'orgnameId',
                rules: [{ required: true, message: "请输入办理部门代码", trigger: "blur" }],
              },
              {
                label: '办件编号',
                prop: 'internalNo',
                rules: [{ required: true, message: "请输入办件编号", trigger: "blur" }],
              },
            ]
          },
          {
            icon: 'el-icon-info',
            label: '发件人信息',
            // arrow: false,
            collapse: true,

            prop: 'group2',
            column: [
              {
                label: '发件人姓名',
                prop: 'sendName',
                rules: [{ required: true, message: "请输入发件人姓名", trigger: "blur" }],
              },
              {
                label: '手机号码',
                prop: 'sendPhone',
                rules: [{ required: true, message: "请输入手机号码", trigger: "blur" }],
              },
              {
                label: '联系电话',
                prop: 'sendCall',
              },
              {
                label: '详细地址',
                prop: 'sendStrect',
                slot: true,
                rules: [{ required: true, message: "请输入详细地址", trigger: "blur" }],
              }
            ],
          },
          {
            icon: 'el-icon-info',
            label: '收件人信息',
            collapse: true,
            prop: 'group1',
            column: [
              {
                label: '收件人姓名',
                prop: 'rcvName',
                rules: [{ required: true, message: "请输入收件人姓名", trigger: "blur" }],
              },
              {
                label: '手机号码',
                prop: 'rcvPhone',
                rules: [{ required: true, message: "请输入手机号码", trigger: "blur" }],
              },
              {
                label: '联系电话',
                prop: 'rcvCall',
              },
              {
                label: '邮编',
                prop: 'rcvPostcode',
                rules: [{ required: true, message: "请输入邮编", trigger: "blur" }],
              },
              {
                label: '详细地址',
                prop: 'rcvStrect',
                rules: [{ required: true, message: "请输入详细地址", trigger: "blur" }]
              },
              {
                label: '邮寄材料说明',
                prop: 'item'
              }
            ]
          },
        ]
      },
      data: [],
      options: dept,
      props: {
        value: 'code',
        label: 'name',
        children: 'children',
        checkStrictly: true
      }
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    async receiveHandler(row) {
      let params = {
        id: row.uuid,
        flag: 99
      }
      let res = await updateIssend(params)
      this.onLoad(this.page);
      this.$message({
        type: "success",
        message: "操作成功!"
      });
      console.log('res=====', res);
    },
    handleChange(value) {
      console.log('=====', this.form.rcvStrectCascader);
    },
    rowSave(row, done, loading) {
      let params = {
        sendProv: this.form.rcvStrectCascader[0],
        sendCity: this.form.rcvStrectCascader[1],
        sendCountry: this.form.rcvStrectCascader[2],
      }
      delete row.rcvStrectCascader
      delete row.sendtime
      delete row.createDate
      row = Object.assign(row, params)
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      delete row.rcvStrectCascader
      delete row.sendtime
      delete row.createDate
      delete row.toZwfwnlptDate
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.uuid).then(res => {
          this.form = res.data.data;
          this.form.rcvStrectCascader = [
            this.form.sendProv,
            this.form.sendCity,
            this.form.sendCountry,
          ]

        });
      }
      if (type === 'view') {
        this.disabled = true
      } else {
        this.disabled = false
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  },
  created() {
  },
};
</script>

<style scoped></style>
